package tech.aistar.config;


import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import tech.aistar.util.mytoken.CreatToken;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Component
public class WxLoginHandlerInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


      if(!(handler instanceof HandlerMethod)){
          return  true;
      }else{
          //通过反射获取注解
          HandlerMethod handlerMethod= (HandlerMethod) handler;
          Method method=handlerMethod.getMethod();//
          //判断接口是否需要登陆
          WxInterceptor guidelInterceptor=method.getAnnotation(WxInterceptor.class);
          //如果有GuidelInterceptor 进行拦截
          if(guidelInterceptor !=null){
              String tokens= (String) request.getAttribute("token");
              String  checkToken= CreatToken.checkToken(tokens);
              if("error".equals(tokens)){
                 return false;
              }else{
                  return true;
              }
          }else{
              return true;
          }



      }
    }
}
